/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BussinessObject;

import DataAccessObject.SQLServerDAO;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ngoctoan
 */
public class NhaphangBO {

    public ResultSet LayIDpn() {
        SQLServerDAO dao;
        try {
            dao = new SQLServerDAO();
            return dao.LayDuLieu("SELECT Id_Nhapxuat FROM tblNhapxuat WHERE Id_Nhapxuat = (SELECT MAX(Id_Nhapxuat) FROM tblNhapxuat)");
        } catch (Exception ex) {
            Logger.getLogger(NhaphangBO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ResultSet LayMasp() {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            return dao.LayDuLieu("SELECT ID_Sanpham, Masp FROM tblSanpham");
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
        return null;
    }

    public ResultSet LayCtsp(Object Masp) {
        try {
            SQLServerDAO dao = new SQLServerDAO();

            return dao.LayDuLieu("SELECT ID_Sanpham, Masp, Tensp, Donvitinh FROM tblSanpham WHERE Masp = '" + Masp + "'");
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
        return null;
    }

    public int ThemCtpn(Object Id_Nhapxuat, Object Id_Sanpham, Object Soluong, Object Gianhap, Object Thanhtien) {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            Object p[] = new Object[5];
            p[0] = Id_Nhapxuat;
            p[1] = Id_Sanpham;
            p[2] = Soluong;
            p[3] = Gianhap;
            p[4] = Thanhtien;
            int kq = dao.UpdateQuery("INSERT INTO tblCtnx VALUES(?,?,?,?,?)", p);
            return kq;

        } catch (Exception ex) {
            Logger.getLogger(NhaphangBO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int CapNhatSPNhapHang(Object Id_Sanpham, Object Soluong, Object Giaban) {

        SQLServerDAO dao;
        try {
            dao = new SQLServerDAO();

            Object p[] = new Object[3];
            p[0] = Soluong;
            p[1] = Giaban;
            p[2] = Id_Sanpham;
            int kq = dao.UpdateQuery("UPDATE tblSanpham SET Soluong=Soluong+?, Giaban=? WHERE ID_Sanpham = ?", p);
            return kq;
        } catch (Exception ex) {
            Logger.getLogger(NhaphangBO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int ThemPhieuNhap(Object Maphieu, Object Loai, Object IdNguoidung, Object Tongtien, Object Ghichu) {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            Object p[] = new Object[6];
            //System.out.println(row+1);
            String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
            //System.out.println(timeStamp);
            p[0] = Maphieu;
            p[1] = Loai;
            p[2] = IdNguoidung;
            p[3] = timeStamp;
            p[4] = Tongtien;
            p[5] = Ghichu;
            //System.out.println(p[1]);
            int kq = dao.UpdateQuery("INSERT INTO tblNhapxuat VALUES(?,?,?,?,?,?)", p);
            int returnId = 1;

            ResultSet rs = this.LayIDpn();
            while (rs.next()) {
                returnId = Integer.parseInt(rs.getString(1));
            }
            System.out.println(String.valueOf(returnId));
            return returnId;
        } catch (Exception e) {
            System.out.println("1" + e);
        }
        return 0;
    }
}
